package com.tz.colawallet.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tz.colawallet.R;
import com.tz.colawallet.basecode.base.BaseFragment;
import com.tz.colawallet.bean.GetTags;
import com.tz.colawallet.bean.Recommand;
import com.tz.colawallet.constant.Key;
import com.tz.colawallet.ui.activity.CommonWebViewActivity;
import com.tz.colawallet.ui.activity.PerfectMessageActivity;
import com.tz.colawallet.ui.adapter.MainScreenAdapter;
import com.tz.colawallet.ui.adapter.MainSecondAdapter;
import com.tz.colawallet.ui.biz.MainSecondFragBiz;
import com.tz.colawallet.ui.component.DaggerMainSecondFragComponent;
import com.tz.colawallet.ui.contract.MainSecondFragContract;
import com.tz.colawallet.ui.module.MainSecondFragModule;
import com.tz.colawallet.ui.presenter.MainSecondFragPresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MainSecondFrag extends BaseFragment implements MainSecondFragContract.View {

    @Inject
    MainSecondFragPresenter presenter;
    @Inject
    MainSecondFragBiz biz;

    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    TextView typeText;
    ImageView typeImg;
    LinearLayout typeLayout;
    TextView moneyText;
    ImageView moneyImg;
    LinearLayout moneyLayout;
    LinearLayout screenLayout;

    private MainSecondAdapter mainSecondAdapter;
    private List<Recommand> recommandList;
    private int clickPosition;
    private Integer loanMoneyId, loanTypeId, page, size = 10;
    private PopupWindow typePopWindow, moneyPopWindow;
    private MainScreenAdapter perfectMessageAdapter;
    private List<GetTags> getTypeList, getMoneyList;
    private int chooseTypePosition, chooseMoneyPosition;

    @Override
    protected void setup() {
        super.setup();
        DaggerMainSecondFragComponent.builder().mainSecondFragModule(new MainSecondFragModule(this)).build().inject(this);
        presenter.setBiz(biz);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_main_second;
    }

    @Override
    protected void initTitle(View view) {

    }

    @Override
    protected void init(View view) {
        recyclerView = view.findViewById(R.id.recycler_view);
        swipeRefreshLayout = view.findViewById(R.id.swipe_refresh_layout);
        typeText = view.findViewById(R.id.type_text);
        typeImg = view.findViewById(R.id.type_img);
        typeLayout = view.findViewById(R.id.type_layout);
        moneyText = view.findViewById(R.id.money_text);
        moneyImg = view.findViewById(R.id.money_img);
        moneyLayout = view.findViewById(R.id.money_layout);
        screenLayout = view.findViewById(R.id.screen_layout);

        recommandList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainSecondAdapter = new MainSecondAdapter(R.layout.item_product, getActivity());
        recyclerView.setAdapter(mainSecondAdapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reloadData();
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        mainSecondAdapter.setOnListener(new MainSecondAdapter.OnListener() {
            @Override
            public void onListener(int position) {
                clickPosition = position;
                //0为已经写过详情，1为未写过
//                if (SPUtils.get(getActivity(), Key.MESSAGE_DETAIL, "1").equals("0")) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString(Key.WEB_URL, recommandList.get(position).getWapurl());
//                    switchActivity(CommonWebViewActivity.class, bundle);
//                } else {
                presenter.addApplyNum(recommandList.get(position).getId());
                presenter.checkDetail();
//                }
            }
        });

        mainSecondAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                presenter.recommand(loanMoneyId, loanTypeId, page, size);
            }
        }, recyclerView);

        typeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTypePop();
                typeText.setTextColor(getResources().getColor(R.color.text_red_color));
                typeImg.setImageResource(R.mipmap.arrow_red_down);
            }
        });
        moneyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moneyText.setTextColor(getResources().getColor(R.color.text_red_color));
                moneyImg.setImageResource(R.mipmap.arrow_red_up);
                showMoneyPop();
            }
        });

        page = 1;
        presenter.recommand(loanMoneyId, loanTypeId, page, size);
        getTypeList = new ArrayList<>();
        getMoneyList = new ArrayList<>();
        presenter.getLoanType();
        presenter.getLoanMoney();
    }

    @Override
    protected void initData() {

    }

    private void reloadData() {
        page = 1;
        recommandList.clear();
        presenter.recommand(loanMoneyId, loanTypeId, page, size);
    }

    @Override
    public Context curContext() {
        return getActivity();
    }

    @Override
    public void showRecommand(List<Recommand> o) {
        recommandList.addAll(o);
        mainSecondAdapter.replaceData(recommandList);
        if (o.isEmpty()) {
            mainSecondAdapter.loadMoreEnd();
        } else {
            mainSecondAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showCheckDetail(Object o) {
        //0为已经写过详情，1为未写过
//        SPUtils.put(getActivity(), Key.MESSAGE_DETAIL, o);
        if (o.equals("0")) {
            Bundle bundle = new Bundle();
            bundle.putString(Key.WEB_URL, recommandList.get(clickPosition).getWapurl());
            switchActivity(CommonWebViewActivity.class, bundle);
        } else {
            switchActivity(PerfectMessageActivity.class, null);
        }
        reloadData();
    }

    @Override
    public void showGetLoanMoney(List<GetTags> o) {
        GetTags getTags = new GetTags();
        getTags.setId(-1);
        getTags.setName("金额不限");
        getTags.setChoose(true);
        getMoneyList.add(getTags);
        getMoneyList.addAll(o);
    }

    @Override
    public void showGetLoanType(List<GetTags> o) {
        GetTags getTags = new GetTags();
        getTags.setId(-1);
        getTags.setName("类型不限");
        getTags.setChoose(true);
        getTypeList.add(getTags);
        getTypeList.addAll(o);
    }

    @Override
    public void showMsg(String msg) {
        showToast(msg);
    }

    private void showTypePop() {
        @SuppressLint("InflateParams") View popupView = getLayoutInflater().inflate(R.layout.item_screen_pop, null);
        typePopWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        typePopWindow.setAnimationStyle(R.style.popup_window_anim);
        typePopWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
        typePopWindow.setFocusable(true);
        typePopWindow.setOutsideTouchable(true);
        typePopWindow.update();
        typePopWindow.showAsDropDown(screenLayout, 0, 0);
        typePopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                typeText.setTextColor(getResources().getColor(R.color.text_gray_color));
                typeImg.setImageResource(R.mipmap.arrow_gray_down);
            }
        });

        RecyclerView recyclerView = popupView.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        perfectMessageAdapter = new MainScreenAdapter(R.layout.item_per_msg, getActivity(), getTypeList.get(chooseTypePosition).getName());
        recyclerView.setAdapter(perfectMessageAdapter);
        perfectMessageAdapter.replaceData(getTypeList);

        perfectMessageAdapter.setOnListener(new MainScreenAdapter.OnListener() {
            @Override
            public void onListener(int position) {
                typeText.setText(getTypeList.get(position).getName());
                if (getTypeList.get(position).getId() != -1) {
                    loanTypeId = getTypeList.get(position).getId();
                } else {
                    loanTypeId = null;
                }
                chooseTypePosition = position;
                for (GetTags getTags : getTypeList) {
                    getTags.setChoose(false);
                }
                getTypeList.get(position).setChoose(true);
                perfectMessageAdapter.notifyDataSetChanged();

                typeText.setTextColor(getResources().getColor(R.color.text_gray_color));
                typeImg.setImageResource(R.mipmap.arrow_gray_down);
                typePopWindow.dismiss();
                reloadData();
            }
        });
    }

    private void showMoneyPop() {
        @SuppressLint("InflateParams") View popupView = getLayoutInflater().inflate(R.layout.item_screen_pop, null);
        typePopWindow = new PopupWindow(popupView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        typePopWindow.setAnimationStyle(R.style.popup_window_anim);
        typePopWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
        typePopWindow.setFocusable(true);
        typePopWindow.setOutsideTouchable(true);
        typePopWindow.update();
        typePopWindow.showAsDropDown(screenLayout, 0, 0);
        typePopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                moneyText.setTextColor(getResources().getColor(R.color.text_gray_color));
                moneyImg.setImageResource(R.mipmap.arrow_gray_up);
            }
        });

        RecyclerView recyclerView = popupView.findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        perfectMessageAdapter = new MainScreenAdapter(R.layout.item_per_msg, getActivity(), getMoneyList.get(chooseMoneyPosition).getName());
        recyclerView.setAdapter(perfectMessageAdapter);
        perfectMessageAdapter.replaceData(getMoneyList);

        perfectMessageAdapter.setOnListener(new MainScreenAdapter.OnListener() {
            @Override
            public void onListener(int position) {
                moneyText.setText(getMoneyList.get(position).getName());
                if (getMoneyList.get(position).getId() != -1) {
                    loanMoneyId = getMoneyList.get(position).getId();
                } else {
                    loanMoneyId = null;
                }
                chooseMoneyPosition = position;
                for (GetTags getTags : getMoneyList) {
                    getTags.setChoose(false);
                }
                getMoneyList.get(position).setChoose(true);
                perfectMessageAdapter.notifyDataSetChanged();

                moneyText.setTextColor(getResources().getColor(R.color.text_gray_color));
                moneyImg.setImageResource(R.mipmap.arrow_gray_up);
                typePopWindow.dismiss();
                reloadData();
            }
        });
    }


}
